import Levels from './rule-level'
import Types from './rule-type'
import Utils from './_utils'
import _ from 'lodash'

const rule = 'email'
const defaultPattern = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/

/*
* config {
*   email: /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/ // Function/String/RegExp
* }
* */

export default {
  priority: Levels.MEDIUM_LEVEL,
  validate: function (value, config, model, formModel, filed, fieldScope) {
    return {
      valid: Utils.validateWrapper(null, defaultPattern, value, config, model, formModel, (params, value, model, formModel) => {
        return _.isNil(value) || value === '' || new RegExp(params).test(value)
      }),
      errorTemplate: undefined,
      errorParams: undefined
    }
  },
  rule: rule,
  type: Types.TYPE
}
